package com.qf.service;

import com.qf.constant.RabbitMQConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 路由策略 根据号码归属地不同 发给不同的运营商
 */
@Service(value = "router")
@Slf4j
public class RouterServiceImpl implements FilerChainService {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private CacheFeign cacheFeign;


    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块 - 路由策略]，开始执行，StandardSubmit={}",standardSubmit);

        //1. 封装队列的名称，根据运营商ID进行封装
        String queueName = RabbitMQConstants.TOPIC_SMS_GATEWAY + standardSubmit.getGatewayID();

        //2. 创建指定的队列
        amqpAdmin.declareQueue(QueueBuilder.durable(queueName).build());

        //3. 发送消息到指定队列(使用默认的交换机)  根据运营商ID  将短信数据发送到对应的运营商队列中
        rabbitTemplate.convertAndSend(queueName,standardSubmit);

        //4. 记录日志,return true
        log.info("[策略模块 - 路由策略]，执行成功，路由数据到={}队列中， StandardSubmit={}",queueName,standardSubmit);
        return true;
    }
}
